package com.xiaoxiang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxiang.auth.entity.User;
import com.xiaoxiang.auth.param.UserParam;
import com.xiaoxiang.auth.service.UserService;
import com.xiaoxiang.common.result.HttpResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author haha
 * @since 2022-03-24 14:41:43
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public HttpResult selectAll(Page<User> page, User user) {
        return HttpResult.success(this.userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping
    public HttpResult selectOne(@RequestParam Long id) {
        return HttpResult.success(this.userService.getUserResultById(id));
    }

    /**
     * 新增数据
     *
     */
    @PostMapping
    public HttpResult insert(@RequestBody UserParam userParam) {
        return HttpResult.success(this.userService.addUser(userParam));
    }

    /**
     * 修改数据
     *
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody UserParam userParam) {
        return HttpResult.success(this.userService.updateUser(userParam));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.userService.removeById(id));
    }
}
